package com.example.cheapshark.mobile;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.net.URLEncoder;


//import org.json.JSONObject;
class CSURLBuilder {
	private String baseURL;
	private String page;
	private String scriptName;
	private ArrayList<Integer> stores;
	private int pageNum;
	private String sortBy;
	private String filter;
	private boolean steamworks;
	private boolean desc;
	private int lowerPrice;
	private int upperPrice;
	private int pageSize;
	public CSURLBuilder(String base, String pageURL, String script)
	{
		baseURL = base;
		page = pageURL;
		scriptName = script;
		stores = new ArrayList<Integer>();
		pageNum = 0;
		sortBy = "";
		filter = "";
		steamworks = false;
		desc = false;
		lowerPrice = 0;
		upperPrice = Integer.MAX_VALUE;
		pageSize = 50;
	}
	public String BuildUrl()
	{
		String output = "";
		try {
			StringBuilder query = new StringBuilder();
			String urlString = "http://" + baseURL + page;
			query.append(urlString);
			query.append("?scriptName=" + URLEncoder.encode(scriptName,"utf-8"));
			for(int i = 0; i < stores.size(); i++)
			{
				query.append("&storeIDs[]=" + stores.get(i));
			}
			query.append("&pageSize=" + pageSize);
			query.append("&page=" + pageNum);
			query.append("&sortBy=" + sortBy);
			query.append("&desc=" + (desc ? 1 : 0));
			query.append("&filter=" + filter);
			query.append("&steamworks=" + (steamworks ? 1: 0));
			query.append("&lowerPrice=" + lowerPrice);
			query.append("&upperPrice=" + upperPrice);
			URL url = new URL(query.toString());
			URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
			output = uri.toString();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return output;
	}
	public boolean isSteamworks() {
		return steamworks;
	}
	public void setSteamworks(boolean steamworks) {
		this.steamworks = steamworks;
	}
	public boolean isDesc() {
		return desc;
	}
	public void setDesc(boolean desc) {
		this.desc = desc;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getBaseURL() {
		return baseURL;
	}
	public String getFilter() {
		return filter;
	}
	public int getLowerPrice() {
		return lowerPrice;
	}
	public String getPage() {
		return page;
	}
	public int getPageNum() {
		return pageNum;
	}
	public String getScriptName() {
		return scriptName;
	}
	public String getSortBy() {
		return sortBy;
	}
	public ArrayList<Integer> getStores() {
		return stores;
	}
	public int getUpperPrice() {
		return upperPrice;
	}
	public void setBaseURL(String baseURL) {
		this.baseURL = baseURL;
	}
	public void setFilter(String filter) {
		this.filter = filter;
	}
	public void setLowerPrice(int lowerPrice) {
		this.lowerPrice = lowerPrice;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public void setScriptName(String scriptName) {
		this.scriptName = scriptName;
	}
	public void setSortBy(String sortBy) {
		this.sortBy = sortBy;
	}
	public void setUpperPrice(int upperPrice) {
		this.upperPrice = upperPrice;
	}
}
